package com.mobileares.midp.widgets.client.page;

import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-3-30
 * Time: 11:57:49
 * To change this template use File | Settings | File Templates.
 */
public class ViewPanel extends Composite {
    private DockPanel frame;
    private DockPanel outFrame;
    private SimplePanel north;
    private SimplePanel west;
    private SimplePanel center;
    private SimplePanel south;
    private SimplePanel east;

    private StackPanel menuPanel;
    private FlowPanel westContainer;
    private FlowPanel northContainer;
    private SimplePanel hl;

    private int num;
    private static int westWidth;
    private int menuWidth =200;
    private Label wel;
    private static int height;

    public ViewPanel() {
        this(60,200);
    }

    public static String getCenterWidth() {
        return Window.getClientWidth() - westWidth + "px";
    }

    public ViewPanel(int bannerHeight,int menuWidth) {
        this.num = bannerHeight;
        this.menuWidth = menuWidth;
        outFrame = new DockPanel();
        outFrame.setWidth("100%");

        frame = new DockPanel();
        frame.setStyleName("view");
        frame.setWidth("100%");

        initNorth();
        initWest();
        initCenter();

        frame.add(west, DockPanel.WEST);
        westWidth = menuWidth;
//        frame.setCellWidth(west, westWidth + "px");
        west.setWidth(westWidth + "px");

        frame.add(center, DockPanel.CENTER);

        outFrame.add(north, DockPanel.NORTH);
        outFrame.setCellHeight(north, num + "px");
        outFrame.add(frame, DockPanel.CENTER);
        initWidget(outFrame);


        Window.addResizeHandler(new ResizeHandler() {
            public void onResize(ResizeEvent event) {
                resize(height);
            }
        });
    }

    private void initNorth() {
        north = new SimplePanel();
        north.setStyleName("north");
        north.setSize("100%", "100%");

        northContainer = new FlowPanel();
        hl = new SimplePanel();
        hl.setStyleName("fish-title-text");
//
        northContainer.add(hl);
        northContainer.add(createTopFrameBar());

        north.add(northContainer);


    }

    public FlowPanel getNorthContainer() {
        return northContainer;
    }

    public void setWelcomeText(String text) {
        wel.setText(text);
    }

    public void setWelcomeBar(String html) {
        HTML h = new HTML(html);
        h.setWordWrap(false);
        h.setHorizontalAlignment(HTML.ALIGN_LEFT);
        setWelcomeBar(h);
    }

    public void setWelcomeBar(Widget widget) {
        hl.setWidget(widget);
    }


    private void initWest() {
        west = new SimplePanel();
        west.setStyleName("west");
//        west.setHeight("100%");
        westContainer = new FlowPanel();
        westContainer.setStyleName("west-div");
//        westContainer.setHeight("100%");
        wel = new Label("欢迎使用");
        wel.setStyleName("login");
        westContainer.add(wel);

        menuPanel = new MyStackPanel();
//        menuPanel.setHeight("90%");
        menuPanel.setStyleName("fish-StackPanel");
        westContainer.add(menuPanel);

//        westContainer.add(createFrameBar());

        west.add(westContainer);


    }

    public SimplePanel getCenter() {
        return center;
    }

    public StackPanel getMenuPanel() {
        return menuPanel;
    }


    private void initCenter() {
        center = new SimplePanel();
        center.setStyleName("center");
        center.setWidth("100%");
//        center.setSize("100%", "100%");
    }

    private SimplePanel createTopFrameBar() {
        SimplePanel bar = new SimplePanel() {
            private boolean open = true;

            @Override
            public void onBrowserEvent(Event event) {
                switch (DOM.eventGetType(event)) {
                    case Event.ONCLICK:
                        if (!open) {
                            setFPVisible(northContainer, true);
                            outFrame.setCellHeight(north, num + "px");
                            resize(num);
                            removeStyleDependentName("close");
                            addStyleDependentName("open");
                            open = true;
                        } else {
                            setFPVisible(northContainer, false);
                            setVisible(true);
                            outFrame.setCellHeight(north, "1px");
                            resize(1);
                            removeStyleDependentName("open");
                            addStyleDependentName("close");
                            open = false;
                        }
                        break;
                }
                super.onBrowserEvent(event);
            }
        };
        bar.sinkEvents(Event.ONCLICK);
        bar.setStyleName("fish-top-frameBar");
        bar.addStyleDependentName("open");

        return bar;

    }

    private void fireHandler(int width) {
        for (CalculateMainWidthEvent ce : cs) {
            ce.calculate(Window.getClientWidth() - width);

        }
    }

    private SimplePanel createFrameBar() {
        SimplePanel bar = new SimplePanel() {
            private boolean open = true;

            @Override
            public void onBrowserEvent(Event event) {
                switch (DOM.eventGetType(event)) {
                    case Event.ONCLICK:
                        if (!open) {
                            setFPVisible(westContainer, true);
                            westWidth = menuWidth;
//                            frame.setCellWidth(west, westWidth + "px");
                            west.setWidth(westWidth + "px");
                            removeStyleDependentName("close");
                            addStyleDependentName("open");
                            open = true;
                            fireHandler(westWidth);
                        } else {
                            setFPVisible(westContainer, false);
                            setVisible(true);
                            westWidth = 12;
//                            frame.setCellWidth(west, westWidth + "px");
                            west.setWidth(westWidth + "px");

                            removeStyleDependentName("open");
                            addStyleDependentName("close");
                            open = false;
                            fireHandler(westWidth);
                        }
//                        frame.setCellWidth(center, Window.getClientWidth() - westWidth + "px");
                        center.setWidth(Window.getClientWidth() - westWidth - 2 + "px");

                        break;
                }
                super.onBrowserEvent(event);
            }
        };
        bar.sinkEvents(Event.ONCLICK);
        bar.setStyleName("fish-frameBar");
        bar.addStyleDependentName("open");

        return bar;

    }

    private void setFPVisible(FlowPanel container, boolean visible) {
        for (int i = 0; i < container.getWidgetCount(); i++) {
            Widget temp = container.getWidget(i);
            temp.setVisible(visible);
        }
    }

    /**
     * A special purpose widget to allow scrollable stack panels.
     */
    public class MyStackPanel extends StackPanel {
        private ArrayList<Widget> scrollers = new ArrayList<Widget>();

        public void insert(Widget w, int before) {
            ScrollPanel p = new ScrollPanel(w);
            p.setWidth("100%");
            scrollers.add(before, p);
            super.insert(p, before);
        }

        public void onLoad() {
            setWidth("96%");
            if (getSelectedIndex() != -1)
                showStack(getSelectedIndex());
        }

        /**
         * Shows the widget at the specified child index.
         *
         * @param index the index of the child to be shown
         */
        public void showStack(int index) {
            super.showStack(index);

            if (this.isAttached()) {
                ScrollPanel me = (ScrollPanel) scrollers.get(index);
                me.setHeight("1px");
                Element tr = DOM.getChild(DOM.getFirstChild(getElement()),
                        index * 2 + 1);
                int trHeight = DOM.getElementPropertyInt(tr, "offsetHeight");
                me.setHeight(trHeight + "px");

            }
        }
    }

    @Override
    protected void onLoad() {
        DeferredCommand.addCommand(new Command() {
            public void execute() {
                resize(num);
            }
        });


    }

    public int getMainWidth() {
        return Window.getClientWidth() - westWidth;
    }

    public int getMainHeight() {
        return Window.getClientHeight() - height - 2;
    }


    private void resize(int num) {
        //init height
        this.height = num;
        int height = Window.getClientHeight() - this.height - 2;
        this.setHeight(Window.getClientHeight() + "px");
        frame.setHeight(height + "px");
        west.setHeight(height + "px");
        center.setHeight(height + "px");
        menuPanel.setHeight(height - 36 + "px");
        if (menuPanel.getSelectedIndex() != -1)
            menuPanel.showStack(menuPanel.getSelectedIndex());
        //init width
//        frame.setCellWidth(center, Window.getClientWidth() - westWidth + "px");
        center.setWidth(Window.getClientWidth() - westWidth - 2 + "px");
        fireHandler(westWidth);

    }

    private static List<CalculateMainWidthEvent> cs = new ArrayList();

    public static void addCalculateEvents(CalculateMainWidthEvent event) {
        cs.add(event);
    }

    public interface CalculateMainWidthEvent {
        /**
         * value:main宽度
         *
         * @param
         */
        void calculate(int value);
    }


}
